/*
 * GLPrivateTrade.h
 *
 *  Created on: Nov 3, 2014
 *      Author: yuliang
 */

#ifndef GLPRIVATETRADE_H_
#define GLPRIVATETRADE_H_

#include "LogicBase/GLContainerBase.h"
#include "GLGlobalDefine.h"

class CGLPrivateTrade : public CGLContainerBase {
public:
	typedef VecGoods_t			VecTradeGoods_t;
private:
	enum UserBoxNumber {
		UserLauncherNum = 1,
		UserBeLauncherNum,
	};
public:
	CGLPrivateTrade();
	virtual ~CGLPrivateTrade();
	virtual inline void Reset() { CGLContainerBase::Reset();
	SetLauncherUser(0, 0); m_LauncherSetGold = 0; m_BelauncherSetGold = 0; }

	inline void SetLauncherUser(const UserId_t& launcher, const UserId_t& belauncher) {
		m_LauncherId = launcher; m_BelaucherId = belauncher; }
	inline const UserId_t& GetLauncherId() const { return m_LauncherId; }
	inline const UserId_t& GetBelauncherId() const { return m_BelaucherId; }
	inline const UserId_t& GetOtherId(const UserId_t& uid) const;

	inline void SetPackGoods(const UserId_t& uid, Index_t index, GoodsInfo& info, const ContainerOperateGoodsInfo& pos);
	inline void SetBodyGoods(const UserId_t& uid, Index_t index, GoodsInfo& info, const ContainerOperateGoodsInfo& pos);
	inline void AddGold(const UserId_t& uid, const Gold_t& gold);
	inline void GetUserGoods(const UserId_t& uid, VecTradeGoods_t& goods) const;
	inline Gold_t GetUserGold(const UserId_t& uid) const;

	inline void SetInvalid() { SetStatus(ENTITY_STATUS_TRADE_INVALID); }
	inline bool IsInvalid() const { return ExistStatus(ENTITY_STATUS_TRADE_INVALID); }
	inline bool UserSure(const UserId_t& uid);

	inline bool IsPackGoods(Index_t index) const;
	inline bool IsBodyGoods(Index_t index) const;
private:
	void setUserGoods(const UserId_t& uid, Index_t index, GoodsInfo& info, const ContainerOperateGoodsInfo& pos);
protected:
	// 新建物品,并初始化
	virtual CGLGoodsBase* getGoods();
	virtual void setGoods(CGLGoodsBase* goods);
	virtual const GoodsFixInfo* getGoodsFixInfo(Type_t type) const;
private:
	UserId_t						m_LauncherId;
	UserId_t						m_BelaucherId;
	Gold_t							m_LauncherSetGold;
	Gold_t							m_BelauncherSetGold;
};

const UserId_t& CGLPrivateTrade::GetOtherId(const UserId_t& uid) const {
	if (m_LauncherId == uid)
		return m_BelaucherId;
	return m_LauncherId;
}

void CGLPrivateTrade::SetPackGoods(const UserId_t& uid, Index_t index, GoodsInfo& info, const ContainerOperateGoodsInfo& pos) {
	if (!IsPackGoods(index))
		SetInvalid();
	setUserGoods(uid, index, info, pos);
}

void CGLPrivateTrade::SetBodyGoods(const UserId_t& uid, Index_t index, GoodsInfo& info, const ContainerOperateGoodsInfo& pos) {
	if (!IsBodyGoods(index))
		SetInvalid();
	setUserGoods(uid, index, info, pos);
}

void CGLPrivateTrade::AddGold(const UserId_t& uid, const Gold_t& gold) {
	if (uid == m_LauncherId) {
		if (gold + m_LauncherSetGold > MAX_COMMODITY_UNIT_PRICE)
			SetInvalid();
		else
			m_LauncherSetGold += gold;
	} else if (uid == m_BelaucherId) {
		if (gold + m_BelauncherSetGold > MAX_COMMODITY_UNIT_PRICE)
			SetInvalid();
		else
			m_BelauncherSetGold += gold;
	} else
		SetInvalid();
}

void CGLPrivateTrade::GetUserGoods(const UserId_t& uid, VecTradeGoods_t& goods) const {
	if (uid == m_LauncherId)
		GetGoods<VecTradeGoods_t, CGLGoodsBase>(UserLauncherNum, goods);
	else if (uid == m_BelaucherId)
		GetGoods<VecTradeGoods_t, CGLGoodsBase>(UserBeLauncherNum, goods);
}

Gold_t CGLPrivateTrade::GetUserGold(const UserId_t& uid) const {
	if (uid == m_LauncherId)
		return m_LauncherSetGold;
	else if (uid == m_BelaucherId)
		return m_BelauncherSetGold;
	return 0;
}

bool CGLPrivateTrade::UserSure(const UserId_t& uid) {
	if (uid == m_LauncherId) {
		if (ExistStatus(ENTITY_STATUS_TRADE_BELAUNCHER_TRUE))
			return true;
		SetStatus(ENTITY_STATUS_TRADE_LAUNCHER_TRUE);
	} else if (uid == m_BelaucherId) {
		if (ExistStatus(ENTITY_STATUS_TRADE_LAUNCHER_TRUE))
			return true;
		SetStatus(ENTITY_STATUS_TRADE_BELAUNCHER_TRUE);
	}
	return false;
}

bool CGLPrivateTrade::IsPackGoods(Index_t index) const {
	return index >= 0 && index < (MAX_CONTAINER_BOX_INDEX_SIZE >> 1);
}

bool CGLPrivateTrade::IsBodyGoods(Index_t index) const {
	return index >= (MAX_CONTAINER_BOX_INDEX_SIZE >> 1) && index < MAX_CONTAINER_BOX_INDEX_SIZE ;
}

#endif /* GLPRIVATETRADE_H_ */
